<template>
  <div class="container">
    <div class="app-container">
      <div class="role-operate">
        <el-button @click="showDialog = true" size="small" type="primary">添加角色</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="list">
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template v-slot="{row}">
            <el-input size="mini" v-model="row.editRow.name" v-if="row.isEdit"></el-input>
            <span v-else>{{row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200px" label="启用">
          <template v-slot="{row}">
            <el-switch
              v-model="row.editRow.state"
              :inactive-value="0"
              :active-value="1"
              v-if="row.isEdit"
            ></el-switch>
            <span v-else>{{row.state === 1?"已启用" : row.state === 0 ? "未启用" : "无"}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{row}">
            <el-input
              type="textarea"
              v-model="row.editRow.description"
              size="mini"
              v-if="row.isEdit"
            ></el-input>
            <span v-else>{{row.description}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <!-- 操作按钮 -->
          <template v-slot="{row}">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button @click="btnEditOk(row)" type="primary" size="mini">确定</el-button>
              <el-button @click="row.isEdit=false" size="mini">取消</el-button>
            </template>
            <template v-else>
              <!-- 非编辑状态 -->
              <el-button size="mini" type="text">分配权限</el-button>
              <el-button @click="btnEditRow(row)" size="mini" type="text">编辑</el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <el-pagination
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          :total="pageParams.total"
          layout="prev,pager,next"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <!-- 弹层 -->
    <el-dialog title="新增角色" width="500px" :visible.sync="showDialog">
      <el-form ref="roleForm" :rules="rules" :model="roleForm" label-width="120px">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" style="width:300px" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="启用" prop="state">
          <el-switch
            v-model="roleForm.state"
            :inactive-value="0"
            :active-value="1"
            style="width:300px"
            size="mini"
          ></el-switch>
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input
            v-model="roleForm.description"
            type="textarea"
            :rows="3"
            style="width:300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="18">
              <el-button @click="btnOk" size="mini" type="primary">确定</el-button>
              <el-button @click="btnCancel" size="mini">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole, updateRole, delRole } from "@/api/role";

export default {
  name: "Role",
  data() {
    return {
      list: [],
      showDialog: false,
      pageParams: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      roleForm: {
        name: "",
        description: "",
        state: 0
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "角色描述不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.pageParams);
      this.list = rows;
      this.pageParams.total = total;
      this.list.forEach(item => {
        this.$set(item, "isEdit", false);
        this.$set(item, "editRow", {
          name: item.name,
          state: item.state,
          description: item.description
        });
      });
    },
    changePage(newPage) {
      this.pageParams.page = newPage;
      this.getRoleList();
    },
    btnOk() {
      this.$refs.roleForm.validate(async isOk => {
        if (isOk) {
          await addRole(this.roleForm);
          this.$message.success("新增角色成功");
          this.getRoleList();
          this.btnCancel();
        }
      });
    },
    btnCancel() {
      this.$refs.roleForm.resetFields();
      this.showDialog = false;
    },
    btnEditRow(row) {
      row.isEdit = true;
      row.editRow.name = row.name;
      row.editRow.state = row.state;
      row.editRow.description = row.description;
    },
    async btnEditOk(row) {
      if (row.editRow.name && row.editRow.description) {
        await updateRole({ ...row.editRow, id: row.id });
        this.$message.success("更新角色成功");
        Object.assign(row, { ...row.editRow, isEdit: false });
      } else {
        this.$message.warning("角色和描述不能为空");
      }
    },
    async confirmDel(id) {
      await delRole(id);
      this.$message.success("删除角色成功");
      if (this.list.length === 1) this.pageParams.page--;
      this.getRoleList();
    }
  }
};
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
